package com.spider.moudle.util;

import com.spider.moudle.constant.Constant;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Linbo Hu on 2017/11/21.
 * 时间工具类
 */
public class DateUtil {

    public static final String yyyy = "yyyy";

    public static final String yyyy_MM = "yyyy-MM";

    public static final String yyyy_MM_dd = "yyyy-MM-dd";

    public static final String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";

    public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    /**
     * 根据不同的pattern将字符串转化为时间
     *
     * @param pattern
     * @param date_string
     * @return
     * @throws ParseException
     */
    public static Date str_to_date(String pattern, String date_string) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        return sf.parse(date_string);
    }

    /**
     * 以不同的pattern将时间转化为字符串
     *
     * @param paramDate
     * @param pattern
     * @return
     */
    public static String date_to_str(Date paramDate, String pattern) {
        if (paramDate == null) {
            return null;
        }
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(pattern);
        localSimpleDateFormat.setLenient(false);
        return localSimpleDateFormat.format(paramDate);
    }

    /**
     * 将时间字符串转化为符合规范的时间字符串格式
     *
     * @param string
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static String str_date_str(String string, String pattern) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        return sf.format(sf.parse(string));
    }

    /**
     * 获取当前时间的字符串
     *
     * @param pattern 不同的显示格式
     * @return
     */
    public static String getNowTimeString(String pattern) {
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        return sf.format(now);
    }

    /**
     * 将时间转化为timestamp
     *
     * @param date
     * @return
     */
    public static Timestamp date_to_timestamp(Date date) {
        return new Timestamp(date.getTime());
    }

    /**
     * @param date
     * @param format
     * @return true 今天   false 不是今天
     */
    public static boolean isToday(String date, String format) {
        Date updateTime = null;
        try {
            updateTime = DateUtil.str_to_date(format, date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date now = new Date();

        return updateTime.getTime() > now.getTime() - Constant.LAST_24_HOUR_TIME_STAMP;
    }

    public static Date parseDateTime(String paramString) {
        if ((paramString == null) || (paramString.trim().equals("")))
            return null;
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
        localSimpleDateFormat.setLenient(false);
        try {
            return localSimpleDateFormat.parse(paramString);
        } catch (Exception localParseException) {
        }
        return null;
    }

    public static String formatDate(Date paramDate) {
        if (paramDate == null)
            return null;
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(yyyy_MM_dd);
        localSimpleDateFormat.setLenient(false);
        return localSimpleDateFormat.format(paramDate);
    }

    public static Date parseDate(String paramString) throws Exception {
        if ((paramString == null) || (paramString.trim().equals("")))
            return null;
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(yyyy_MM_dd);
        localSimpleDateFormat.setLenient(false);
        try {
            return localSimpleDateFormat.parse(paramString);
        } catch (Exception localParseException) {
            throw new Exception("日期解析出错！", localParseException);
        }
    }


}
